load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_zig//zig:defs.bzl", "zig_library")

config_setting(
    name = "darwin_arm64",
    constraint_values = [
        "@platforms//os:macos",
        "@platforms//cpu:aarch64",
    ],
)

config_setting(
    name = "darwin_amd64",
    constraint_values = [
        "@platforms//os:macos",
        "@platforms//cpu:x86_64",
    ],
)

cc_library(
    name = "empty",
    defines = ["ZML_RUNTIME_CPU_DISABLED"],
)

copy_to_directory(
    name = "sandbox",
    srcs = select({
        ":darwin_arm64": ["@libpjrt_cpu_darwin_arm64//:libpjrt_cpu"],
        ":darwin_amd64": ["@libpjrt_cpu_darwin_amd64//:libpjrt_cpu"],
        "@platforms//os:linux": ["@libpjrt_cpu_linux_amd64//:libpjrt_cpu"],
    }),
    out = "sandbox/lib",
    include_external_repositories = ["**"],
)

cc_library(
    name = "libpjrt_cpu",
    data = [":sandbox"],
    defines = ["ZML_RUNTIME_CPU"],
)

zig_library(
    name = "cpu",
    import_name = "runtimes/cpu",
    main = "cpu.zig",
    visibility = ["//visibility:public"],
    deps = [
        "//pjrt",
    ] + select({
        "//runtimes:cpu.enabled": [
            ":libpjrt_cpu",
            "//async",
            "//stdx",
            "@rules_zig//zig/runfiles",
        ],
        "//conditions:default": [":empty"],
    }),
)

filegroup(
    name = "layers",
    srcs = [],
    visibility = ["//visibility:public"],
)
